#include "VethData.h"
#include "ManageProxy.h"
#include "configure.h"
#include "TaskTime.h"
#include <unistd.h>
#include "common.h"
#include "MacType.h"
#include "proxyEncrypt.h"
#include "TcpServerImpl.h"


#define lanproxy_version  "lanproxy_version:1.1.5"

 ManageProxy *g_pproxy_20230802 = nullptr;
void statisticsThread()
{
    //统计发送包数量
    while (1) {
        g_pproxy_20230802->showStatistics();       
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
}

int main(int argc, char* argv[]) 
{
    if ((argc == 2) && (strcmp(argv[1], "-v") == 0)){
        printf("version : %s  build data : %s \n\n", lanproxy_version, __DATE__);
        return 0;
    }

    if (argc != 2) {
        printf("usage : ./bin/lanproxy ./etc/proxy_config.json \n\n");
        return -1;
    }

    //读取配置文件，初始化config
    if (0 != configure::instance()->load(argv[1])){
        printf("load config fail : %s \n ", argv[1]);
        return -1;
    }

    SLT_setLogConfig("./etc/config-caclog.ini");
    SLT_logInit();
    
    SLT_LOG_INFO("%s", lanproxy_version);
    SLF_init(2);
    CCacTcpServer::getInst()->init();

    //ManageProxy proxy;
    g_pproxy_20230802 = new ManageProxy();
    int ret = g_pproxy_20230802->init();
    int ret2 = 0;
    //密钥协商
    if(1 == configure::instance()->get_encrypt()){
        ret2 = CProxyEncryptManage::getInst()->init(g_pproxy_20230802);
		SLT_LOG_WARM("Need to negotiate key");
	}
    
    if (ret == 0 && ret2 == 0) {
        SLT_LOG_INFO("lanproxy run success");
        std::thread t1(statisticsThread);

        while (1) {
            SLT_sleep(2000);
        }
    }
    else{
        SLT_LOG_ERR("lanproxy run error:ret:%d,ret2:%d\n ",ret,ret2);
        SLT_sleep(2000);
    }

    return(0);
}